package com.zs.leymovie.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.handmark.pulltorefresh.library.ILoadingLayout;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.bitmap.PauseOnScrollListener;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.HttpHandler;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.zs.leymovie.BaseApp;
import com.zs.leymovie.MainActivity;
import com.zs.leymovie.R;
import com.zs.leymovie.activity.MovieDetailActivity;
import com.zs.leymovie.adapter.MovingAdapter;
import com.zs.leymovie.bean.MovieBanner;
import com.zs.leymovie.bean.MovieBannerEntity;
import com.zs.leymovie.bean.MovieDataEntity;
import com.zs.leymovie.bean.MovieShowing;
import com.zs.leymovie.constants.UrlConstants;
import com.zs.leymovie.flashview.FlashView;
import com.zs.leymovie.flashview.constants.EffectConstants;
import com.zs.leymovie.utils.HttpHelper;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Created by Administrator on 2015/8/18 0018.
 * 作者：舒纲聿
 * 时间：2015年8月18日14:43:58
 * 功能；正在放映
 */
public class MovingFragment extends Fragment implements AdapterView.OnItemClickListener, PullToRefreshBase.OnRefreshListener2 {

    @ViewInject(R.id.movie_item_fg_container)
    private PullToRefreshListView mPtrListview;
    private BitmapUtils bitmapUtils = new BitmapUtils(BaseApp.getInstance());
    private ViewHolder vh;
    private ArrayList<MovieBanner> movieBanners = new ArrayList<>();
    private ArrayList<MovieShowing> movieShowings = new ArrayList<>();
    private MovingAdapter movingAdapter;
    private HttpHandler iHandler;
    private HttpHandler mHandler;
    private String url;
    private String city_id;
    private FragmentManager manager;
    private TypeReference<MovieDataEntity<MovieShowing>> type = new TypeReference<MovieDataEntity<MovieShowing>>() {
    };

    public MovingFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_movie_base_item, container, false);
        ViewUtils.inject(this, view);
        initView();
        initData();
        return view;
    }

    private void initData() {
        city_id = ((MainActivity) getActivity()).getCity_id();
        url = UrlConstants.getOnlineUrl(city_id);
        if (url != null && url.length() > 0) {
            mHandler = HttpHelper.getInstance().get(url, movieShowings, movingAdapter, mPtrListview, type);
        }
        getBannerData();
    }

    //初始化listview
    private void initView() {
        mPtrListview.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
        //获得刷新的view
        ListView listView = mPtrListview.getRefreshableView();

        //加载头部布局和底部布局
        addView(listView);

        ILoadingLayout loadingLayout = mPtrListview.getLoadingLayoutProxy();

        //listview在快速滑动时默认不加载图片
        mPtrListview.setOnScrollListener(new PauseOnScrollListener(bitmapUtils, true, false));

        //设置上下拉刷新监听
        mPtrListview.setOnRefreshListener(this);
        //设置点击事件
        mPtrListview.setOnItemClickListener(this);
        //上下拉动时展现view
        ILoadingLayout headLayout = mPtrListview.getLoadingLayoutProxy(true, false);
        headLayout.setLoadingDrawable(BaseApp.getInstance().getResources().getDrawable(R.mipmap.shouye_loading));
        ILoadingLayout footerLayout = mPtrListview.getLoadingLayoutProxy(false, true);

        //设置适配器
        movingAdapter = new MovingAdapter(movieShowings, getActivity());
        mPtrListview.setAdapter(movingAdapter);

        mPtrListview.setRefreshing();
    }

    private void addView(ListView listView) {
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View headView = inflater.inflate(R.layout.movie_lv_head_vp, null);
        View footerView = inflater.inflate(R.layout.movie_lv_footer_text, null);
        listView.addHeaderView(headView);
        vh = new ViewHolder(headView);
        listView.addFooterView(footerView);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(getActivity(), MovieDetailActivity.class);
        intent.putExtra("movie", movieShowings.get(position - 2));
        intent.putExtra("flag", "Moving");
        intent.putExtra("city_id", city_id);
        startActivity(intent);
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {
        if (url != null && url.length() > 0) {
            mHandler = HttpHelper.getInstance().get(url, movieShowings, movingAdapter, mPtrListview, type);
        }
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {

    }


    private class ViewHolder {
        @ViewInject(R.id.movie_vp_container)
        private FlashView flashView;

        public ViewHolder(View v) {
            ViewUtils.inject(this, v);
        }

    }

    private void getBannerData() {
        HttpUtils utils = new HttpUtils();
        iHandler = utils.send(HttpRequest.HttpMethod.GET, UrlConstants.getBannerUrl(city_id), new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                String info = responseInfo.result;
                MovieBannerEntity entity = JSON.parseObject(info, MovieBannerEntity.class);
                if (entity.getStatus() == 1 && entity.getData() != null) {
                    movieBanners.addAll((Collection<? extends MovieBanner>) entity.getData());
                    getImageUrls();
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {
                UrlConstants.getPrompt();
            }
        });
    }

    private void showRotation(List<String> imageUrls) {
        FlashView view = vh.flashView;
        if (imageUrls != null && imageUrls.size() > 0) {
            view.setVisibility(View.VISIBLE);
            view.setImageUris(imageUrls);
            view.setEffect(EffectConstants.ZOOM_OUT_PAGE_EFFECT);//更改图片切换的动画效果
        } else {
            view.setVisibility(View.GONE);
        }
    }

    private void getImageUrls() {
        ArrayList<String> imageUrls = new ArrayList<>();
        if (movieBanners != null && movieBanners.size() > 0) {
            for (int i = 0; i < movieBanners.size(); i++) {
                imageUrls.add(movieBanners.get(i).getPromotion_img_url());
            }
            showRotation(imageUrls);
        }else{
            vh.flashView.setVisibility(View.GONE);
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        if (iHandler != null) {
            iHandler.cancel();
        }
        if (mHandler != null) {
            mHandler.cancel();
        }
    }
}
